// pages/index/addOrderInfo/index.js
import Common from '../../../utils/common.js'
let http = new Common()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index99: false,//蒙版层
    InputName: '',
    InputPhone: '',
    tel: '',//获取火锅
    //火锅
    data1: '',
    NewMoney:0,//总价格
    oldMoney:0,//火锅的价格
    MoneyDetailShow:false,//费用详情Modle

  },
  index99:function(){
    this.setData({
      index99:false,
      MoneyDetailShow:false
    })
  },
  BInputPhone: function (e) {

  },
  BInputPhoneBlur:function(e){
    let that=  this
    if ((/^1[34578]\d{9}$/.test(e.detail.value))) {
      this.setData({
        InputPhone: e.detail.value
      })
    } else {
      wx.showModal({
        title: '',
        content: '请输入正确电话号码',
        complete(){
          that.setData({
            InputPhone: ''
          })
        }
      })
    }
  },
  BInputName: function (e) {
    // if ((/^1[34578]\d{9}$/.test(e.detail.value))) {
    //   this.setData({
    //     InputPhone: e.detail.value
    //   })
    // }

    this.setData({
      InputName: e.detail.value
    })
  },
  BInputRemark: function (e) {
    this.setData({
      InputRemark: e.detail.value
    })
  },
  //火锅
  getData1: function () {
    let storeid = this.data.oldInfo.list.id
    let that = this
    let tel = this.data.tel
    http.http({
      url: 'mp2/store/hotpot',
      data: {
        storeid: storeid,
        tel: tel
      },
      success(res) {
        let data = res.data.data
        let aa = []
        let bb = []

        for (let i in data.hotpot) {
          aa.push(data.hotpot[i])
        }
        aa.forEach(val => {
          val.num = 0
        })
        let note = data.note
        note.forEach(val => {
          bb.push({ name: val, data1Index: false })
        })

        that.setData({
          data1: {
            hotpot: aa,
            note: note,
            newnote: bb
          }
        })

        // console.log(data.note)
      }
    })
  },
  //总价钱
  money: function () {
    let hotpot = this.data.data1.hotpot
    let money = 0
    hotpot.forEach(val => {
      val.money = (parseFloat(val.discount_price) * val.num)
      money += parseFloat(val.money)
    })

    this.setData({
      NewMoney: (parseFloat(this.data.oldInfo.money) + money).toFixed(2),
      oldMoney: money
    })
    // console.log(this.data.NewMoney)
  },
  //点击火锅
  data1Click: function (e) {
    let index = e.currentTarget.dataset.index
  },
  delete1: function (e) {
    let index = e.currentTarget.dataset.index
    let hotpot = this.data.data1.hotpot

    let i = hotpot[index].num - 1
    let aa = `data1.hotpot[${index}].num`
    if ((i + 1) > 0) {
      this.setData({
        [aa]: i
      })
    }
    this.money()
  },
  add1: function (e) {
    let index = e.currentTarget.dataset.index
    let hotpot = this.data.data1.hotpot

    let i = hotpot[index].num + 1
    let aa = `data1.hotpot[${index}].num`
    this.setData({
      [aa]: i
    })
    this.money()
  },
  Remarks: function (e) {
    let index = e.currentTarget.dataset.index
    let data = `data1.newnote[${index}].data1Index`

    let aa = this.data.data1.newnote[index].data1Index

    let bb = !aa
    // console.log(index)
    // console.log(bb)

    this.setData({
      [data]: bb
    })

    // console.log(this.data.data1.newnote[index])
  },
  //费用明细
  MoneyDetail:function(){
    this.setData({
      index99: true,
      MoneyDetailShow: true
    })
  },
  formSubmit: function (e) {
    let formId = e.detail.formId
    let good = []
    
    this.data.data1.hotpot.forEach(val=>{
      if(val.num>0){
        good.push({ id: val.id, num: val.num })
      }
    })
     let goods = JSON.stringify(good)
    let remark = ''
    this.data.data1.newnote.forEach(val => {
      if (val.data1Index){
        remark += `${val.name},`
      }
    })

    // console.log(remark)
    let data = {
      viptel: this.data.tel,
      starttime: this.data.oldInfo.Bom_date_Detail,
      endtime: this.data.oldInfo.Bom_date_Detail2,
      storeid: this.data.oldInfo.list.id,
      roomtype: this.data.oldInfo.Room.type,
      singertel: this.data.InputPhone,
      singername: this.data.InputName,
      roomprice: this.data.oldInfo.money,
      hotpotprice: this.data.oldMoney,
      ordertype: this.data.oldInfo.index,
      goods: goods,
      dang: this.data.oldInfo.nextIndex,
      remark: remark,
      formId: formId
    }

    http.http({
      method: 'post',
      data:data,
      url: 'mp2/store/createorder',
      success(res){
        let id = JSON.stringify(res.data.data)
        http.goUrl({
          url: `../../payment/cancelPay/index?id=${res.data.data.roomorderno}`
        })
        console.log(res)
      },
    })

    // console.log(data)

    // console.log(this.data.data1)
    // console.log(this.data.oldInfo)
    // console.log(this.data.InputName)
    // console.log(this.data.InputPhone)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let aa = {
      Bom_date_Detail: "2018-10-24 13:30",
      Bom_date_Detail2: "2018-10-24 16:00",
      index: "0",
      money: '20',
      Room: {
        maxPeople:
          5,
        minPeople
          :
          1,
        title
          :
          "小包",
        type
          :
          2
      },
      list: {
        address
          :
          "四川省眉山市青神县康苑世纪城 4楼",
        comment
          :
          0,
        distance
          :
          "82.98km",
        id
          :
          1081,
        image: {
          1: "http://wuwei-tech.com:8090/image/1081/img/2a06f496b7f513f40ab5f8d58da34ebf.jpg"
        },

        lat
          :
          29.846113,
        long
          :
          103.855881,
        lowerPrice
          :
          "0.00",
        minImage
          :
          "http://wuwei-tech.com:8090/image/1081/img/d8ca086b8391d203b245ce64748b58f4.jpg",
        star
          :
          5,
        tel
          :
          "18121911281",
        title
          :
          "音乐派-青神店"
      },
      nextIndex:'0'
    }
    
    let id = JSON.parse(options.id) || aa
    let tel = wx.getStorageSync('tel')
    let myInfo = JSON.parse(wx.getStorageSync('myInfo'))
    console.log(id)
    this.setData({
      oldInfo: id,
      tel: tel,
      InputPhone: tel,
      InputName: myInfo.nickName,
      NewMoney: id.money
    })
    // console.log(id)
    // console.log(aa)
    this.getData1()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})